package com.multi.tenant.util;

import com.multi.common.core.domain.AjaxResult;
import com.multi.common.core.domain.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * @Description 调用Http请求
 * @ClassName HttpUtils
 * @Author pzj
 * @Data 2024/11/18 13:55
 */
@Component
public class HttpServiceUtils {

    @Autowired
    private RestTemplate restTemplate;


    /**
     * 远程调用post方法（含有Token）
     * @param url
     * @param token
     * @param object
     * @return
     */
    public AjaxResult doPost(String url,String token,Object object){
        HttpHeaders headers=new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json");
        headers.setContentType(type);
        headers.add("Accept", MediaType.ALL_VALUE);
        headers.add("Authorization", token);
        //进行远程调用创建用户信息
        HttpEntity<? extends Object> formEntity= new HttpEntity<>(object, headers);
        AjaxResult ajaxResult = restTemplate.postForObject(url, formEntity, AjaxResult.class);
        return ajaxResult;
    }

    /**
     * 远程调用post方法（不含有Token）
     * @param url
     * @param object
     * @return
     */
    public AjaxResult doPost(String url,Object object){
        HttpHeaders headers=new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json");
        headers.setContentType(type);
        headers.add("Accept", MediaType.ALL_VALUE);
        //进行远程调用创建用户信息
        HttpEntity<? extends Object> formEntity= new HttpEntity<>(object, headers);
        AjaxResult ajaxResult = restTemplate.postForObject(url, formEntity, AjaxResult.class);
        return ajaxResult;
    }
}
